﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interactive.Utility;

namespace TravelLink.Common {
    public sealed class Utility {


        public static string SiteRoot = System.Configuration.ConfigurationManager.AppSettings["siteurl"];



        //定义密码字符的范围,小写、大写字母、大小写混合、数字以及特殊字符
        private static readonly string lowerChars = "abcdefghijklmnopqrstuvwxyz";
        private static readonly string upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static readonly string mixedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private static readonly string numnberChars = "0123456789";
        private static readonly string specialCahrs = "~!@#$%^&*:;,.?";   //"\" 转义字符不添加 "号不添加

        /// <summary>
        /// 生成各类随机密码,包括纯字母,纯数字,带特殊字符等,除非字母大写密码类型,其余方式都将采用小写密码
        /// </summary>
        /// <param name="pwdType">密码类型 大写为"UPPER",小写为"LOWER",数字为"NUMBER",字母与数字为"NUMCHAR",数字字母字符都包括为"ALL" </param>
        /// <param name="length">密码长度</param>
        /// <returns></returns>
        public static string RandomPassword(PwdType pwdType, int length) {

            string tmpStr = "";
            int iRandNum;
            Random rnd = new Random();
            length = (length < 6) ? 6 : length; //密码长度必须大于6,否则自动取6
            // LOWER为小写 UPPER为大写 NUMBER为数字 NUMCHAR为数字和字母 ALL全部包含 五种方式
            //只有当选择UPPER才会有大写字母产生,其余方式中的字母都为小写,避免有些时候字母不区分大小写
            if (pwdType == PwdType.LOWER) {
                for (int i = 0; i < length; i++) {
                    iRandNum = rnd.Next(lowerChars.Length);
                    tmpStr += lowerChars[iRandNum];
                }
            }
            else if (pwdType == PwdType.UPPER) {
                for (int i = 0; i < length; i++) {
                    iRandNum = rnd.Next(upperChars.Length);
                    tmpStr += upperChars[iRandNum];
                }
            }
            else if (pwdType == PwdType.NUMBER) {
                for (int i = 0; i < length; i++) {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
            }
            else if (pwdType == PwdType.NUMCHAR) {
                int numLength = rnd.Next(length);
                //去掉随机数为0的情况
                if (numLength == 0) {
                    numLength = 1;
                }
                int charLength = length - numLength;
                string rndStr = "";
                for (int i = 0; i < numLength; i++) {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
                for (int i = 0; i < charLength; i++) {
                    iRandNum = rnd.Next(mixedChars.Length);
                    tmpStr += mixedChars[iRandNum];
                }
                //将取得的字符串随机打乱
                for (int i = 0; i < length; i++) {
                    int n = rnd.Next(tmpStr.Length);
                    //去除n随机为0的情况
                    //n = (n == 0) ? 1 : n;
                    rndStr += tmpStr[n];
                    tmpStr = tmpStr.Remove(n, 1);
                }
                tmpStr = rndStr;
            }
            else if (pwdType == PwdType.ALL) {
                int numLength = rnd.Next(length - 1);
                //去掉随机数为0的情况
                if (numLength == 0) {
                    numLength = 1;
                }
                int charLength = rnd.Next(length - numLength);
                if (charLength == 0) {
                    charLength = 1;
                }
                int specCharLength = length - numLength - charLength;
                string rndStr = "";
                for (int i = 0; i < numLength; i++) {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
                for (int i = 0; i < charLength; i++) {
                    iRandNum = rnd.Next(lowerChars.Length);
                    tmpStr += lowerChars[iRandNum];
                }
                for (int i = 0; i < specCharLength; i++) {
                    iRandNum = rnd.Next(specialCahrs.Length);
                    tmpStr += specialCahrs[iRandNum];
                }
                //将取得的字符串随机打乱
                for (int i = 0; i < length; i++) {
                    int n = rnd.Next(tmpStr.Length);
                    //去除n随机为0的情况
                    //n = (n == 0) ? 1 : n;
                    rndStr += tmpStr[n];
                    tmpStr = tmpStr.Remove(n, 1);
                }
                tmpStr = rndStr;
            }
            //默认将返回数字类型的密码
            else {
                for (int i = 0; i < length; i++) {
                    iRandNum = rnd.Next(numnberChars.Length);
                    tmpStr += numnberChars[iRandNum];
                }
            }
            return tmpStr;
        }
    }

    public enum PwdType {
        /// <summary>
        /// 小写英文字母
        /// </summary>
        LOWER = 0,

        /// <summary>
        /// 大写英文字母
        /// </summary>
        UPPER,

        /// <summary>
        /// 数字
        /// </summary>
        NUMBER,

        /// <summary>
        /// 字母和数字混合
        /// </summary>
        NUMCHAR,

        /// <summary>
        /// 字母数字字符混合
        /// </summary>
        ALL
    }
}
